#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
struct MTA {
  string name;
  set<string> users;
};
vector<MTA> mtas;
map<string, int> mtaid;

struct Sender {
  string mta;
  string name;
};
struct Receiver {
  string mta;
  vector<string> users;
};
void parse(string t, string& server, string& user) {
  for (char& c : t)
    if (c == '@') c = ' ';
  stringstream ss(t);
  ss >> user >> server;
}
void send(Sender& sender, vector<Receiver>& receivers, vector<string> content) {
  for (Receiver rec : receivers) {
    MTA& mta1 = mtas[mtaid[sender.mta]];
    MTA& mta2 = mtas[mtaid[rec.mta]];
    // mta
    cout << "Connection between " << sender.mta;
    cout << " and " << rec.mta << endl;
    // sender
    cout << "     HELO " << sender.mta << endl;
    cout << "     250" << endl;
    cout << "     MAIL FROM:<" << sender.name << '@' << sender.mta << '>'
         << endl;
    cout << "     250" << endl;
    // receiver
    int cnt = 0;
    for (string user : rec.users) {
      cout << "     RCPT TO:<" << user << '@' << rec.mta << '>' << endl;
      if (mta2.users.count(user))
        cout << "     250" << endl, cnt++;
      else
        cout << "     550" << endl;
    }
    // content
    if (cnt) {
      cout << "     DATA" << endl;
      cout << "     354" << endl;
      for (string line : content) {
        cout << "     " << line << endl;
      }
      cout << "     ." << endl;
      cout << "     250" << endl;
    }
    // quit
    cout << "     QUIT" << endl;
    cout << "     221" << endl;
  }
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // freopen("1.txt", "r", stdin);
  // freopen("2.txt", "w", stdout);
  string t;
  while (cin >> t && t != "*") {
    MTA mta;
    cin >> mta.name;
    int cnt;
    cin >> cnt;
    rep(i, 1, cnt) {
      string user;
      cin >> user;
      mta.users.insert(user);
    }
    mtas.push_back(mta);
    mtaid[mta.name] = mtas.size() - 1;
  }
  while (cin >> t && t != "*") {
    Sender sender;
    parse(t, sender.mta, sender.name);
    vector<Receiver> receivers;
    while (cin >> t && t != "*") {
      string server, user;
      parse(t, server, user);
      bool found = false;
      for (Receiver& rec : receivers) {
        if (rec.mta == server) {
          for (string x : rec.users) {
            if (x == user) {
              found = true;
              break;
            }
          }
          if (!found) rec.users.push_back(user);
          found = true;
          break;
        }
      }
      if (!found) receivers.push_back(Receiver{server, vector<string>{user}});
    }
    vector<string> content;
    string line;
    while (getline(cin, line)) {
      if (line.empty()) continue;
      if (line == "*") break;
      content.push_back(line);
    }
    send(sender, receivers, content);
  }
  return 0;
}